package com.mls.guardian.appst.chain.excutor;

import com.mls.guardian.appst.chain.TaskExecutor;
import com.mls.guardian.appst.chain.TaskInvoker;
import com.mls.guardian.appst.chain.TaskResult;
import com.mls.guardian.appst.shell.ShellScriptExecutorFuture;
import com.mls.guardian.common.FileUtil;
import com.mls.guardian.dataobj.ModuleMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 2014-400 on 2015-10-26.
 */
public class CopyJarExcutor implements TaskExecutor {
    private static final Logger log = LoggerFactory.getLogger(CopyJarExcutor.class);

    @Override
    public TaskResult exec(TaskInvoker invoker) {
        TaskResult result = new TaskResult();
        try {
            log.info("----开始copy jar----");
            for (ModuleMessage item : invoker.getModuleList()) {
                log.info("----copy jar[{}]----", item.getModuleName());
                String sourceFolder = invoker.getProject().getProjectPath() + File.separator + item.getModuleName() + File.separator + "build" + File.separator + "libs";
                String targetFolder = invoker.getConfig().getCopyDest() + File.separator + invoker.getProject().getProjectName();

                if (Files.notExists(Paths.get(sourceFolder))) {
                    //目录结果不存在,可能是直接放的jar包
                    //复制jar包
                    String sourceFile = invoker.getProject().getProjectPath() + File.separator + item.getModuleName() + ".jar";
                    String targetFile = invoker.getConfig().getCopyDest() + File.separator + invoker.getProject().getProjectName() + File.separator + item + ".jar";

                    if (Files.notExists(Paths.get(targetFile).getParent())) {
                        Files.createDirectories(Paths.get(targetFile).getParent());
                    }
                    Files.copy(Paths.get(sourceFile), Paths.get(targetFile), StandardCopyOption.REPLACE_EXISTING);

                    //复制lib目录
                    sourceFolder = invoker.getProject().getProjectPath() + File.separator + "lib";
                    targetFolder = invoker.getConfig().getCopyDest() + File.separator + invoker.getProject().getProjectName() + File.separator + "lib";
                    if (Files.notExists(Paths.get(targetFolder))) {
                        Files.createDirectories(Paths.get(targetFolder));
                    } else {
                        FileUtil.deleteFolder(targetFolder);
                    }
                    FileUtil.copyFolder(sourceFolder, targetFolder, true);
                } else {
                    if (Files.notExists(Paths.get(targetFolder))) {
                        Files.createDirectories(Paths.get(targetFolder));
                    }
                    FileUtil.copyFolder(sourceFolder, targetFolder, true);
                }
            }

            log.info("----copy jar[{}]----", true);
            result.setSuc(true);
        } catch (IOException e) {
            log.info("----copy jar[{}]----", false);
            result.setSuc(false);
            result.setError(e.getMessage());

            e.printStackTrace();
        } finally {
            log.info("----结束copy jar----");
        }

        if (result.isSuc() == true) {
            return invoker.invoke();
        }

        return result;
    }
}
